<template>
	<view class="box">
		<!-- 轮播图 -->
			<swiper class="swi" indicator-color="white" indicator-active-color="red" :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" circular>
				<swiper-item v-for="(item,index) in goods_info.pics" :key="index">
					<image :src="item.pics_big"  @click="imagepre(index)"></image>
				</swiper-item>
			</swiper>
			
		<view class="content" v-if="goods_info.goods_price">
			<view class="price">
				<text>￥{{goods_info.goods_price}}</text>
			</view>
			
			<view class="name">
				<view class="good_name">
					{{goods_info.goods_name}}
				</view>
				
				<div class="icon">
					<uni-icons type="star" size="18" color="gray"></uni-icons>
					<text>收藏</text>
				</div>
			</view>
			
			<view class="yf">运费.包邮</view>
			
			
		</view>
			
			
			<rich-text :nodes="goods_info.goods_introduce"></rich-text>
			
			
			<uni-goods-nav class="goods_nav" :fill="true"  :options="options" :buttonGroup="buttonGroup"  @click="onClick" @buttonClick="buttonClick" />
	</view>
</template>

<script>
	import { mapState,mapMutations,mapGetters } from 'vuex'
	export default {
		data() {
			return {
				goods_info: {},
				 options: [ {
				             icon: 'shop',
				             text: '店铺',
				             infoBackgroundColor:'#007aff',
				             infoColor:"red"
				         }, {
				             icon: 'cart',
				             text: '购物车',
				             info: 0
				         }],
				         buttonGroup: [{
				           text: '加入购物车',
				           backgroundColor: '#ff0000',
				           color: '#fff'
				         },
				         {
				           text: '立即购买',
				           backgroundColor: '#ffa200',
				           color: '#fff'
				         }
				         ]
			};
		},
		computed: {
			...mapState(['cart']),
			...mapGetters(['CartTotal'])
		},
		watch: {
			// CartTotal(value) {
			// 	let findReaults = this.options.find( x => x.text === '购物车')
			// 	if (findReaults) {
			// 		findReaults.info = value
			// 	}
			// }
			CartTotal: {
				handler(value) {
					let findReaults = this.options.find( x => x.text === '购物车')
						if (findReaults) {
							findReaults.info = value
						}
				},
				immediate: true
			}
		},
		methods: {
			...mapMutations(['add']),
			async GetShopDetails (goods_id) {
				const { data: res } = await uni.$http.get('/api/public/v1/goods/detail',{goods_id})
				console.log(res)
				if (res.meta.status !== 200) return uni.$showmsg()
				this.goods_info = res.message
				this.goods_info.goods_introduce = this.goods_info.goods_introduce.replace(/ <img /g,'<img style="display:block;"')
				this.goods_info.goods_introduce = this.goods_info.goods_introduce.replace(/ webp /g,'jpg')
				console.log(this.goods_info)
			}, 
			onClick (e) {
				if (e.content.text === '购物车') {
					uni.switchTab({
						url: '../../pages/cart/cart'
					})
				}
				console.log(e)
			},
			imagepre (index) {
				console.log(this.goods_info.pics.map(x => x.pics_big))
				uni.previewImage({
					current: index,
					urls: this.goods_info.pics.map(x => x.pics_big)
				})
			},
			buttonClick (e) {
				if (e.content.text === '加入购物车') {
					let goods = {
						goods_id: this.goods_info.goods_id,
						goods_name: this.goods_info.goods_name,
						goods_price: this.goods_info.goods_price,
						goods_count:	this.goods_info.goods_count || 0,
						goods_small_logo: this.goods_info.goods_small_logo,
						goods_state: true
					}
					this.add(goods)
				}
			}
		},
		onLoad(options) {
			const goods_id = options.goods_id
			this.GetShopDetails(goods_id)
		},
		
	}
</script>

<style lang="scss">
.swi {
	height: 750rpx;
}
image {
	width: 100%;
	height: 100%;
}

.content {
	padding: 0 10px;
	margin-top: 30rpx;
	.price {
		font-size: 18px;
		color: #C00000;
	}
	.name {
		margin-top: 10rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		.good_name {
			display: flex;
			margin-right: 10rpx;
			align-items: center;
			justify-content: center;
		}
		.icon {
			display: flex;
			align-items: center;
			justify-content: center;
			flex-direction: column;
			width: 160px;
			border-left: 1px solid gray;
		}
		
		
	}
	.yf {
		margin: 15px 0;
		font-size: 12px;
		color: gray;
	}
	
}
.goods_nav {
	position: fixed;
	bottom: 0;
	left: 0;
	width: 100%;
}
.box {
	padding-bottom: 50px;
}
</style>
